package com.wargame.gaming_data_center;

import java.util.HashMap;
import java.util.List;

/**
 * 态势更新方法，根据态势模型更新最新的推演信息
 *
 * @param thisObsv 态势对象
 * */
public void Update(Observation thisObsv) throws ClassNotFoundException {

    List<HashMap<String,Object>> oprs = thisObsv.getOperators(); // 获取算子数据，所有数据格式遵守算子模型
    List<HashMap<String,Object>> actions = thisObsv.getActions(); // 获取动作数据，所有数据格式遵守动作模型
    List<HashMap<String, Object>> targets = thisObsv.getTargets(); // 获取目标数据，所有数据格式遵守目标模型
    List<HashMap<String, Object>> judgeInfoes = thisObsv.getJudgeinfoes();// 获取裁决信息数据，所有数据格式遵守裁决信息模型
    double currentTime = thisObsv.getUpdateTime();  // 获取态势信息更新时间（逻辑推演时间

//        HashMap<Integer,List<String>> validActions = (HashMap<Integer,List<String>>)ChangeType.ConvertString(getattributes.get("ValidActions"),"HashMap<Integer,List<String>>");
//        HashMap<String,Integer> scores = (HashMap<String,Integer>)ChangeType.ConvertString(getattributes.get("Scores"),"HashMap<String,Integer>");

    //UpdateScores
    // 初始时刻，更新最大分数
    HashMap<String,Integer> scores = new HashMap<>();
    int RedRemainMax = 0, BlueRemainMax = 0;
    if (!oprs.isEmpty()){ 
        for (HashMap<String,Object> opr : oprs) {
            String oprColor = (String) opr.get("Color");
            int oprValue = (int) opr.get("Value");
            int oprMaxBlood = (int) opr.get("MaxBlood");

            if (oprColor.equals("Red")) RedRemainMax += oprValue * oprMaxBlood;// 分值×最大血量
            else if (oprColor.equals("Blue")) BlueRemainMax += oprValue * oprMaxBlood;
        }
    }

    scores.put("RedRemainMax", RedRemainMax);
    scores.put("BlueRemainMax", BlueRemainMax);

    // 更新进攻得分
    int redRemain = 0, blueRemain = 0;
    if (!oprs.isEmpty()){ 
        for (HashMap<String,Object> opr : oprs) {

            String oprColor = (String) opr.get("Color");
            int oprValue = (int) opr.get("Value");
            int oprBlood = (int) opr.get("Blood");
            if (oprColor.equals("Red")) redRemain += oprValue * oprBlood;
            else if (oprColor.equals("Blue")) blueRemain += oprValue * oprBlood;
        }
    }
    scores.put("RedAttack", scores.get("BlueRemainMax") - blueRemain);
    scores.put("BlueAttack", scores.get("RedRemainMax") - redRemain);

    // 更新夺控得分
    int RedOccupy = 0, BlueOccupy = 0;
    if (!targets.isEmpty()){ 
        for (HashMap<String,Object> target : targets) {
            String targetType = (String) target.get("Type");
            if (!targetType.equals("City")) continue;
            String cityFlag = (String) target.get("Flag");
            //dynamic cityFlag = target.GetAttri("Flag");
            if (cityFlag.equals("Red")) {
    
                RedOccupy += (int) target.get("Value");// 每个红方的夺控点的夺控点分数的和
    
            } else if (cityFlag.equals("Blue")) {
    
                BlueOccupy += (int) target.get("Value");// 每个蓝方的夺控点的夺控点分数的和
            }
        }
    }    
    scores.put("RedOccupy", RedOccupy);
    scores.put("BlueOccupy", BlueOccupy);
    scores.put("RedTotal", scores.get("RedAttack") + scores.get("RedOccupy"));
    scores.put("BlueTotal", scores.get("BlueAttack") + scores.get("BlueOccupy"));


    //UpdateValidActions
    HashMap<Integer,List<String>> validActionMap = new HashMap<>();
    if (!oprs.isEmpty()){ 
        for (HashMap<String,Object> opr : oprs) {
            int oprID = (int) opr.get("ObjID");
            List<String> validActions = (List<String>) opr.get("ValidActions");
            boolean isDead = (boolean) opr.get("IsDead");
            if (!isDead) validActionMap.put(oprID,validActions);
        }
    }

    // 更新
    thisObsv.setItem("Operators",oprs);
    thisObsv.setItem("Actions",actions);
    thisObsv.setItem("Targets",targets);
    thisObsv.setItem("Scores",scores);
    thisObsv.setItem("ValidActions",validActionMap);
    thisObsv.setItem("Time", currentTime);
}
